package game.state.gameplay;

import engine.Displays;
import engine.input.Keys;
import engine.input.Mouse;
import engine.render.GLManager;
import engine.render.buffer.TextBuffer;
import engine.render.buffer.TriBuffer;
import engine.res.Animation;
import engine.res.Color;
import engine.res.Res;
import game.state.GameState;
import libRender.Box;
import libRender.Quad;

public class Gameplay extends GameState 
{
    
    private Player pl;
    private boolean showRunInfo;
    private Animation nums;
    private Animation courier;
    
    public Gameplay(Displays _display, GLManager glManager) 
    {
        super(_display);
        pl = new Player();
        showRunInfo = true;
        
        glManager.bindShader("vertLight");
        
        nums = new Animation("nums", 10);
        courier = new Animation("anim02", 96);
    }
    
    public void reset() 
    {
        movingOn = GameState.FALSE;
        pl.reset();
    }
    
    public void update(
            Mouse mouse, Keys keys, 
            TriBuffer dim3, TriBuffer dim2, 
            TextBuffer textBuffer, 
            GLManager glManager, 
            float timeTotal, float timeCycle, int fpsTotal)
    {
        pl.update(mouse, keys, timeCycle);
        
        glManager.getShader().bindUniform("lightPos", pl.getViewX(), pl.getViewY(), pl.getViewZ());
        glManager.bindRotation(pl.rot().rots());
        glManager.bindTranslation(-pl.getViewX(), -pl.getViewY(), -pl.getViewZ());
        
        dim3.triangles(
            Box.gen().get(
                -1.0f, 1.0f, 6.0f, 
                0.625f, 1.0f, 0.625f, 
                Color.WHITE, 
                Res.loader().getTex("ceiling")
//                nums.getCurrentFrame()
//                courier.getFrameAt('P' - 32)
//                courier.getCurrentFrame()
            )
        );
        
        if (keys.wasReleased(Keys.ONE))
            glManager.bindShader("basic");
        if (keys.wasReleased(Keys.TWO))
            glManager.bindShader("basicTex");
        if (keys.wasReleased(Keys.THREE))
            glManager.bindShader("noise");
        if (keys.wasReleased(Keys.FOUR))
            glManager.bindShader("fog");
        if (keys.wasReleased(Keys.FIVE))
            glManager.bindShader("fragLight");
        if (keys.wasReleased(Keys.SIX))
            glManager.bindShader("vertLight");
        
        if (keys.wasReleased(Keys.F1)) 
        {
            showRunInfo = !showRunInfo;
        }
        if (keys.wasReleased(Keys.F2)) 
        {
            pl.reset();
        }
        if (keys.wasPressed(Keys.F3)) 
        {
            pl.physEnabled = !pl.physEnabled;
        }
        
        if (showRunInfo) 
        {
            dim2.triangles(
                Quad.gen().getGenericXY(
                    840.0f, 525.0f, 1.0f, 
                    32.0f, 32.0f, 
                    true, 
                    Color.WHITE, 
                    Res.loader().getTex("crosshair")
                )
            );
            int n = 0;
            int xpos = 10;
            textBuffer.text(xpos, getRasterH(n++), "(F1) Hide Run Info");
            textBuffer.text(xpos, getRasterH(n++), "(F2) Reset Position");
            textBuffer.text(xpos, getRasterH(n++), "(F3) Bool Switch: " + pl.physEnabled);
            n++;
            textBuffer.text(xpos, getRasterH(n++), "FPS: " + fpsTotal);
//            textBuffer.text(xpos, getRasterH(n++), "AA: " + Displays.antialias + "x");
//            textBuffer.text(xpos, getRasterH(n++), "FOV: " + Displays.fov);
//            textBuffer.text(xpos, getRasterH(n++), "TimeTotal: " + timeTotal);
            textBuffer.text(xpos, getRasterH(n++), "Pos: " + pl.pos().pos());
            textBuffer.text(xpos, getRasterH(n++), "Dir: " + pl.rot().dirLook());
            textBuffer.text(xpos, getRasterH(n++), "Shader: " + glManager.getShader().getShaderName());
            textBuffer.text(xpos, getRasterH(n++), "nTris sent:     " + dim3.nTrisSent());
            textBuffer.text(xpos, getRasterH(n++), "nTris rendered: " + dim3.nTrisRendered());
        }
    }
    
    public void perSecond() 
    {
        courier.update();
        nums.update();
    }
    
}
